<template>
  <div>
    <!-- 订货单审核-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTables
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjHeadquartersAudit"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)" :disabled="isOpen">审批通过</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(2)" :disabled="isOpen">审批驳回</multiuse-button>
      </template>
    </FilterTables>
    <BaseDialog :visible.sync="isEmit" :title="'编辑'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :type="type" @close="ownercloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isreject" :title="'驳回意见'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <reject :list="selectedList" @close="rejectclose" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, updateAllocateApply } from '@/api/omSale/allocation/HeadquartersAudit/index.js';
import { dictionary } from '@/utils/dictMixin';
import ownerSeacher from './ownerSeacher';
import { exportExcelFile } from '@/utils';
import emitModel from './emitModel';
import { getAppId } from '@/assets/js/auth'; //获取登录用户
import reject from './reject';
export default {
  name: 'WarehouseMaintenance',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        applyStatus: 60121002
      },
      fetchApi: getList,
      formData: {},
      iswarehouse: false,
      isEmit: false,
      issearch: false,
      fromDealer: '',
      toDealer: '',
      type: 0,
      isOpen: true,
      selectedList: [],
      isreject: false
    };
  },
  mixins: [dictionary],
  components: {
    emitModel,
    ownerSeacher,
    reject
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    if (getAppId() == 'beijing') {
      this.isOpen = false;
    }
  },
  created() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '调拨单号',
          fieldName: 'applyNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '调出方',
          fieldName: 'dealerName',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.fromDealer = '';
            }
          }
        },
        {
          type: 'INPUT',
          label: '调入方',
          fieldName: 'todealerName',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.toDealer = '';
            }
          }
        },

        {
          type: 'RANGE_DATE',
          label: '调出申请时间',
          fieldName: 'startApplyDate|endApplyDate',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)} disabled={props.row.applyStatus != 60121002}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6012'),
          editItems: this.createDictList('6012')
        },
        {
          title: '调拨单号',
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出方',
          dataIndex: 'fromDealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调入方',
          dataIndex: 'toDealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调入方经销商代码',
          dataIndex: 'toDealer',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出方申请时间',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '调拨人',
          dataIndex: 'applyPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调拨说明',
          dataIndex: 'applyRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    seacherFun(type) {
      this.type = type;
      this.issearch = true;
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        let isOpen = true;
        row.map(x => {
          if (x.applyStatus != 60121002) {
            isOpen = false;
          }
        });
        this.isOpen = isOpen ? false : true;
        this.selectedList = row;
      } else {
        this.selectedList = [];
        this.isOpen = true;
        if (getAppId() == 'beijing') {
          this.isOpen = false;
        }
      }
    },
    // async getStorageList(){
    //   let res=await getStorageList()
    //   console.log(res,'res')
    //   this.topFilterList.map(x=>{
    //     if(x.fieldName==''){
    //       x.itemList=res.data.map(x=>({text:'',value:''}))
    //     }
    //   })
    // },
    async addHandle(type) {
      let warnStr = '';
      if ((!this.selectedList || this.selectedList.length == 0) && getAppId() == 'beijing') {
        warnStr = '所有数据';
      }
      let isAll = 0;
      if (warnStr != '') {
        isAll = 1;
      }
      if (type == 1) {
        await this.$confirm('是否通过' + warnStr + '?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectedList.map(x => {
              arr.push(x.id);
            });
            let res = await updateAllocateApply({ applyStatus: 60121003, ids: arr, isAll: isAll, isCheck: 1 });
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '通过成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } else {
        await this.$confirm('是否驳回' + warnStr + '?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.isreject = true;
        });
      }
    },
    changeHandle(params) {
      params.fromDealer = this.fromDealer;
      params.toDealer = this.toDealer;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    ownercloseHandler(val, type) {
      if (val) {
        console.log(val);
        if (type == 1) {
          this.topFilterList.map(x => {
            if (x.fieldName == 'dealerName') {
              x.initialValue = val.dealerName;
              this.fromDealer = val.dealerCode;
            }
          });
        } else {
          this.topFilterList.map(x => {
            if (x.fieldName == 'todealerName') {
              x.initialValue = val.dealerName;
              this.toDealer = val.dealerCode;
            }
          });
        }
      }
      this.issearch = false;
    },
    editHandle(data, type) {
      this.formData = data;
      this.isEmit = true;
    },
    emitcloseHandler(val) {
      this.isEmit = false;
      if (val) {
        this.fetchParams.fromDealer = this.fromDealer;
        this.fetchParams.toDealer = this.toDealer;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    rejectclose(val) {
      this.isreject = false;
      if (val) {
        this.fetchParams.fromDealer = this.fromDealer;
        this.fetchParams.toDealer = this.toDealer;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    resetChange() {
      this.fromDealer = '';
      this.fetchParams.fromDealer = '';
      this.fetchParams.toDealer = '';
      this.toDealer = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
